package p.ithorns.sample.influxdb;

/**
 * ReflectTest
 *
 * @author HtL
 * @date 2025/1/20 9:10
 * @since 1.0.0
 */

import p.ithorns.sample.influxdb.core.BeanUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

public class ReflectTest {

    private final static int nums = 100_000;


    private Long testReflectMethod() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < nums; i++) {
            try {
                // Class clazz = Class.forName("p.ithorns.sample.influxdb.ReflectEntity");
                // Object instance = clazz.newInstance();
                // Method method = clazz.getDeclaredMethod("getReflectMethod");

                 Object instance = BeanUtil.instance(ReflectEntity.class);
                 Method method = BeanUtil.getClassMethod(ReflectEntity.class, "getReflectMethod");
                method.invoke(instance);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
        return System.currentTimeMillis() - startTime;
    }

    private Long testReflectField() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < nums; i++) {
            ReflectEntity reflectEntity = new ReflectEntity("Lxlxxx", 20, "coding");
            Field field = null;
            try {
                //  field = reflectEntity.getClass().getDeclaredField("name");
                field = BeanUtil.getClassField(reflectEntity.getClass(), "name");
                field.set(reflectEntity, "changeLxlxxx");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return System.currentTimeMillis() - startTime;
    }

    private Long testNormMethod() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < nums; i++) {
            ReflectEntity reflectEntity = new ReflectEntity("Lxlxxx", 20, "coding");
            reflectEntity.setName("changeLxlxxx");
        }
        return System.currentTimeMillis() - startTime;
    }

    private Long testNormField() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < nums; i++) {
            ReflectEntity reflectEntity = new ReflectEntity("Lxlxxx", 20, "coding");
            reflectEntity.name = "English";
        }
        return System.currentTimeMillis() - startTime;
    }

    public static void main(String[] args) {
        ReflectTest reflectTest = new ReflectTest();
        final Long aLong = reflectTest.testReflectMethod();
        final Long aLong1 = reflectTest.testReflectField();
        final Long aLong2 = reflectTest.testNormMethod();
        final Long aLong3 = reflectTest.testNormField();
        System.out.println("testReflectMethod执行时间：" + aLong);
        System.out.println("testReflectField执行时间：" + aLong1);
        System.out.println("testNormMethod执行时间：" + aLong2);
        System.out.println("testNormField执行时间：" + aLong3);
    }
}